import SwiftUI

struct ConversationListView: View {
    @ObservedObject var viewModel: ChatViewModel
    
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack {
                Text("对话")
                    .font(.headline)
                    .foregroundColor(.gray)
                
                Spacer()
                
                Button(action: {
                    viewModel.createNewSession()
                }) {
                    Image(systemName: "plus")
                        .foregroundColor(.blue)
                }
            }
            .padding(.horizontal)
            .padding(.top, -3)
            .padding(.bottom, 2)
            
            ScrollView {
                VStack(spacing: 0) {
                    ForEach(viewModel.sessions) { session in
                        SessionRow(
                            session: session,
                            isSelected: viewModel.currentSession.id == session.id,
                            onSelect: { viewModel.switchSession(to: session.id) },
                            onDelete: { viewModel.deleteSession(id: session.id) }
                        )
                    }
                }
            }
        }
        .frame(width: 200)
        .background(Color(red: 0.95, green: 0.95, blue: 0.95))
    }
}

struct SessionRow: View {
    let session: ChatSession
    let isSelected: Bool
    let onSelect: () -> Void
    let onDelete: () -> Void
    
    var body: some View {
        HStack {
            Text(session.title)
                .lineLimit(1)
                .padding(.vertical, 8)
                .foregroundColor(isSelected ? .primary : .gray)
            
            Spacer()
            
            if isSelected {
                Button(action: onDelete) {
                    Image(systemName: "trash")
                        .foregroundColor(.red)
                        .font(.caption)
                }
            }
        }
        .padding(.horizontal)
        .background(isSelected ? Color.blue.opacity(0.1) : Color.clear)
        .contentShape(Rectangle())
        .onTapGesture {
            onSelect()
        }
    }
} 